package app.controller;

import app.domain.User;
import app.vo.ResultResponse;
import app.service.ServiceUser;
import app.utils.RedisTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static app.utils.SendPhoneUtil.getPhoneMsg;
import static app.utils.SendPhoneUtil.getRandom;

@RestController
//处理跨域请求
@CrossOrigin
@RequestMapping("/ForgetPassword")
public class ForgetPasswordController {
    @Autowired
    private ServiceUser serviceUser;
    @Autowired
    private ResultResponse resultResponse;
    @Autowired
    private RedisTemplateUtils redisTemplateUtils;

    //验证用户方法
    @RequestMapping("verifyUser")
    public ResultResponse verifyUser(User user, Boolean state) {
        //查询数据库匹配用户名
        User query = serviceUser.query(user.getUsername());
        //判断当前用户名数据库里面是否有对应的用户名
        if (query == null) {
            resultResponse.setMessage("The user name was not found");
            return resultResponse;
        } else {
            if (user.getName() != null) {
                if (!user.getName().equals(query.getName())) {
                    resultResponse.setMessage("The name is incorrect");
                    resultResponse.setData(query.getId());
                    return resultResponse;
                }
            }
            if (user.getPhone() != null) {
                if (!user.getPhone().equals(query.getPhone())) {
                    resultResponse.setMessage("Incorrect mobile phone number");
                    resultResponse.setData(query.getId());
                    return resultResponse;
                }
            }
            if (state != null) {
                if (state == true) {
                    //第一步发送验证码
                    phoneCode(user.getPhone(), query.getId(), false);
                    this.resultResponse.setMessage("Sent successfully");
                    this.resultResponse.setData(query.getId());
                    return this.resultResponse;
                }
            }
            if (user.getName() == null & user.getPhone() == null) {
                resultResponse.setMessage("next step");
                resultResponse.setData(query.getId());
                return resultResponse;
            }
        }
        serviceUser.update(user);
        resultResponse.setMessage("Modification completed");
        resultResponse.setData(query.getId());
        return resultResponse;
    }

    //验证手机验证码
    @RequestMapping("phoneCodeVerification")
    public ResultResponse phoneCodeVerification(String phoneCode, String phone, Integer id) {
        String code = (String) redisTemplateUtils.get("phoneCode");
        if (code == null) {
            phoneCode(phone, id, false);
            code = (String) redisTemplateUtils.get("phoneCode");
        }
        //判断当用户输入的验证码与发送的验证匹配的时候通过验证并修改密码否则重复这一步
        if (phoneCode.equals(code)) {
            resultResponse.setMessage("The verification code is correct");
            resultResponse.setData(id);
            return resultResponse;
        } else {
            resultResponse.setMessage("Mobile phone verification code error");
            resultResponse.setData(id);
            return resultResponse;
        }
    }

    //发送验证码
    @RequestMapping("phoneCode")
    public ResultResponse phoneCode(String phone, Integer id, Boolean state) {
        //生成随机数
        String code;
        if (state != true) {
            //从redis数据库中取出验证码信息 如没有跳过这个判断
            code = (String) redisTemplateUtils.get("phoneCode");
            //当redis中有验证码信息的情况运行
            if (code != null) {
                //当电话不为空的时候
                if (!phone.equals("")) {
                    //调用方法
                    String phoneMsg =  getPhoneMsg(phone, code);
                    System.out.println(phoneMsg);
                    System.out.println(phone+"::"+code);
                    resultResponse.setMessage("Sent successfully");
                    resultResponse.setData(id);
                    return resultResponse;
                }
            } else {
                //新建一个验证码信息存入redis用于后续验证 验证码
                code = getRandom(6);
                redisTemplateUtils.set("phoneCode", code, 5000);
                //调用方法
                String phoneMsg =  getPhoneMsg(phone, code);
                System.out.println(phoneMsg);
                System.out.println(phone+"::"+code);
                resultResponse.setMessage("Sent successfully");
                resultResponse.setData(id);
                return resultResponse;
            }
        } else {
            //生成随机数
            code = getRandom(6);
            redisTemplateUtils.set("phoneCode", code, 5000);
            //调用方法
            String phoneMsg = getPhoneMsg(phone, code);
            System.out.println(phoneMsg);
            System.out.println(phone+"::"+code);
            resultResponse.setMessage("Refresh");
            resultResponse.setData(id);
            return resultResponse;
        }
        return null;
    }
}